package lanQiaoBei.贪心;

import java.util.Arrays;
import java.util.Comparator;

/*
 * 实现最小字典序列['abc','adb','acb','drt']返回一个互相拼接的最小字典序
 * */
public class minDicPre {
    public static void main(String[] args) {
        String[] strs = new String[]{"a", "b", "hh", "fg", "de","j","ji"};
        String r = generateMinDicPre(strs);
        System.out.println(r);
    }

    //贪心
    private static String generateMinDicPre(String[] strs) {
        if (strs.length == 1) return strs[0];
        Arrays.sort(strs,new ThisComparator());
        String res="";
        for (int i = 0; i < strs.length; i++) {
            res+=strs[i];
        }
        return res;
    }
    static class ThisComparator implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            return (o1+o2).compareTo(o2+o1);
        }
    }
}
